package com.ebay.contest.gfour.common;

import java.util.ArrayList;
import java.util.List;

import com.ebay.services.finding.Amount;
import com.ebay.services.finding.ListingInfo;
import com.ebay.services.finding.SearchItem;
import com.ebay.services.finding.SellerInfo;
import com.ebay.services.finding.SellingStatus;

/**
 * ApiResult to be returned to the web GUI side.
 * 
 */

public class ApiResult {
    // id is the key used for api query, e.g. category id when querying by category,
    // keywords when querying by keywords
    private String id;
    private List<ApiResultItem> items = new ArrayList<ApiResultItem>();

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public List<ApiResultItem> getItems() {
        return items;
    }
}

/**
 * an item in the search result. although we already have a {@link SearchItem} from the ebay Finding API SDK, we don't
 * need all those fields for our GFour web GUI.
 */
class ApiResultItem {
    private String itemId;
    private String title;
    private String itemUrl;
    private String galleryUrl;
    private String primaryCategory;
    private String secondaryCategory;
    private String sellerUserName;
    private String buyItNowPrice;
    private String currentPrice;
    private String positiveFeedback;
    // location
    private double longitude;
    private double latitude;

    public ApiResultItem() {
        // empty
    }

    public ApiResultItem(SearchItem item) {
        this.itemId = item.getItemId();
        this.title = item.getTitle();
        this.itemUrl = item.getViewItemURL();
        this.galleryUrl = item.getGalleryURL();
        this.primaryCategory = item.getPrimaryCategory() != null ? item.getPrimaryCategory().getCategoryName() : null;
        this.secondaryCategory = item.getSecondaryCategory() != null ? item.getSecondaryCategory().getCategoryName()
                : null;
        SellerInfo sellerInfo = item.getSellerInfo();
        if (sellerInfo != null) {
            this.positiveFeedback = sellerInfo.getPositiveFeedbackPercent().toString();
            this.sellerUserName = sellerInfo.getSellerUserName();
        }
        ListingInfo listingInfo = item.getListingInfo();
        if (listingInfo != null) {
            Amount price = listingInfo.getBuyItNowPrice();
            if (price != null) {
                this.buyItNowPrice = String.format("%.2f %s", price.getValue(), price.getCurrencyId());
            }
        }
        SellingStatus sellingStatus = item.getSellingStatus();
        if (sellingStatus != null) {
            Amount price = sellingStatus.getCurrentPrice();
            if (price != null) {
                this.currentPrice = String.format("%.2f %s", price.getValue(), price.getCurrencyId());
            }
        }
    }

    public String getItemId() {
        return itemId;
    }

    public String getTitle() {
        return title;
    }

    public double getLongitude() {
        return longitude;
    }

    public double getLatitude() {
        return latitude;
    }

    public String getItemUrl() {
        return itemUrl;
    }

    public String getGalleryUrl() {
        return galleryUrl;
    }

    public String getPrimaryCategory() {
        return primaryCategory;
    }

    public String getSecondaryCategory() {
        return secondaryCategory;
    }

    public String getSellerUserName() {
        return sellerUserName;
    }

    public String getPositiveFeedback() {
        return positiveFeedback;
    }

    public String getBuyItNowPrice() {
        return buyItNowPrice;
    }

    public void setLatitude(double latitude) {
        this.latitude = latitude;
    }

    public void setLongitude(double longitude) {
        this.longitude = longitude;
    }

    public String getCurrentPrice() {
        return currentPrice;
    }
}
